package com.xyq.system.manager;

import com.xyq.common.core.constants.CacheConstants;
import com.xyq.common.redis.service.RedisService;
import com.xyq.system.domain.exam.Exam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ExamCacheManager {

    @Autowired
    private RedisService redisService ;  // 操作redis

    // 1. 把竞赛存储到 redis里
    public void addCache(Exam exam){
        //1. 把竞赛id存储到列表里
        redisService.leftPushForList(getExamListKey() , exam.getExamId()); // 通过投头插,插入竞赛数据
        //2. 把竞赛的基本信息存储到redis
        redisService.setCacheObject(getDetailKey(exam.getExamId()),exam);
    }
    //2. 删除竞赛缓存,删除竞赛题目缓存
    public void deleteCache(Exam exam){
        //1. 把竞赛id从列表里删除
        redisService.leftPushForList(getExamListKey() , exam.getExamId()); // 通过投头插,插入竞赛数据
        //2. 通过key把竞赛的基本信息删除
        redisService.deleteObject(getDetailKey(exam.getExamId()));

        //3.  删除竞赛题目列表缓存
         redisService.deleteObject(getExamQuestionListKey(exam.getExamId()));
    }

    // 获取竞赛题目列表的key
    public String getExamQuestionListKey(Long examId){
        return CacheConstants.EXAM_QUESTION_LIST+examId ;
    }

    // 获取 exam列表的key
    private String getExamListKey(){
        return CacheConstants.EXAM_UNFINISHED_LIST ;
    }
    // 获取exam竞赛的key
    private String getDetailKey(Long examId){
        // 竞赛的key 是 : e:d: + examId
        return CacheConstants.EXAM_DETAIL+examId;
    }
}
